/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.shareorder.rest;

import com.xunyin.annotation.AnonymousAccess;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.security.security.TokenUtil;
import com.xunyin.modules.security.security.vo.JwtUser;
import com.xunyin.modules.shareorder.domain.TShareOrder;
import com.xunyin.modules.shareorder.service.TShareOrderService;
import com.xunyin.modules.shareorder.service.dto.TShareOrderQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * @author admin
 * @date 2022-09-22
 */
@AllArgsConstructor
@Api(tags = "共享订单管理")
@RestController
@RequestMapping("api")
public class TShareOrderController {

    private final TShareOrderService tShareOrderService;
    private final TokenUtil tokenUtil;

    @GetMapping(value = "/tShareOrder")
    @Log("查询共享订单")
    @ApiOperation("查询共享订单")
    //@PreAuthorize("@el.check('admin','tShareOrder:list')")
    public ResponseEntity getTShareOrders(HttpServletRequest request, TShareOrderQueryCriteria criteria, Pageable pageable) {
        //订单状态查询,如果查询的是全部状态订单，则OrderStatus设置为空
        if (criteria.getOrderStatus() != null) {
            switch (criteria.getOrderStatus()) {
                case -9:
                    criteria.setOrderStatus(null);
                    break;
                default:
            }
        }
//        if (criteria.getStoreId() == 0) {
//            criteria.setStoreId(null);
//        }
        criteria.setStoreId(null);
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            criteria.setStoreId(user.getStoreIdList());
        }
        Map<String, Object> queryAll = tShareOrderService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @PostMapping
    @Log("新增共享订单")
    @ApiOperation("新增共享订单")
    @PreAuthorize("@el.check('admin','tShareOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TShareOrder resources) {
        return new ResponseEntity<>(tShareOrderService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改共享订单")
    @ApiOperation("修改共享订单")
    @PreAuthorize("@el.check('admin','tShareOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TShareOrder resources) {
        tShareOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除共享订单")
    @ApiOperation("删除共享订单")
    @PreAuthorize("@el.check('admin','tShareOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id -> {
            tShareOrderService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 获取包间订单统计数据
     * @return OrderStatDto
     */
    @GetMapping(value = "/data/getOrderStat")
    @AnonymousAccess
    public ResponseEntity getOrderStat(HttpServletRequest request, @RequestParam("storeId") Integer storeId) {
        return new ResponseEntity<>(tShareOrderService.getOrderStat(storeId), HttpStatus.OK);
    }

}
